/**
 * 
 */
package com.cdhospital.admincontroller;

import java.util.Date;
import java.util.List;

import com.cdhospital.Model.Department;
import com.cdhospital.Model.Expert;
import com.cdhospital.Model.News;
import com.cdhospital.adminservice.ExpertService;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * @author 17994
 *
 */
public class AdminExpertController extends Controller {
	static ExpertService service=new ExpertService();
	public void index(){
		Integer pageNumber=getParaToInt("pageNum");
		Integer department=getParaToInt("department");
		if(pageNumber==null){
			pageNumber=1;
		}
       if(department!=null){
    	   setAttr("expert", service.paginate(getParaToInt(0, pageNumber), 10,department));
    	   System.out.println(service.paginate(getParaToInt(0, pageNumber), 10,department).getList());
		}
       else{
    		setAttr("expert", service.paginates(getParaToInt(0, pageNumber), 10));
       }
	    
		setAttr("department", Db.find("select * from department "));
		setAttr("name", "专家风采");
		setAttr("departments", department);
		render("index.html");
	}
	public void form(){	
		Expert expert=null;
		Integer id=getParaToInt("id");	
		if (id!=null && id>0) {
			setAttr("expert", service.findById(id));
		 expert= service.findById(id);
			setAttr("name",expert.getName());
		}
		
		setAttr("department", Db.find("select * from department "));
	
		render("info.html");
	}
	public void save(){
		Expert expert=getModel(Expert.class,"expert");
	if (expert.get("id")==null) {
			
		expert.save();
		}
		else if (expert.getInt("id")>0) {
			expert.update();
		}
		redirect("/admin/expert");
	}
	
	public void delect(){
		Integer id=getParaToInt("id");
		if (id!=null && id>0) {
			boolean flag=service.deleteById(id);
			if (!flag) {
				renderText("删除失败");
				setAttr("err", 2);
				return;
			}
			
		}
		else {
			renderText("删除失败");
			setAttr("err", 2);
			return;
		}
		
		   renderJson();
	}
	public void serch(){
	
		String title=getPara("title");
		title="%"+title+"%";
	   List<Record> expert=Db.find("select expert.*, department.* from expert,department where expert.name like ?",title);
	   System.out.println(expert);
		setAttr("department", Db.find("select * from department "));
		setAttr("name", "专家风采");
	    setAttr("expert",expert);
	    render("index.html");
	}
}
